home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
098
/
bal.c
< prev
next >
Wrap
Text File
|
1985-06-03
|
4KB
|
125 lines
#include <stdio.h>
/****************************************************************/
/* Balance comments, quotes, ifdefs, parens, and braces in */
/* C programs. Ed Post, Wilsonville OR, 8-18-84 */
/****************************************************************/
main(argc,argv)
INT16 argc;
char **argv;
{
FILE *f,*fopen();
int lines,comments,parens,braces,ifdefs,singleQuote,doubleQuote,c;
int commentLines[25],parenLines[100],braceLines[100],ifdefLines[10];
char buf[100], *p;
if (!--argc) {
printf("usage: bal <file> ... ");
exit(1);
}
while (argc--) {
argv++;
if ((f=fopen(*argv,"r"))==NULL) {
printf("can't open file.\n");
exit(1);
}
lines=parens=braces=comments=ifdefs=singleQuote=doubleQuote=0;
while (fgets(buf,100,f)!=NULL) {
lines++;
if (strncmp(buf,"#ifdef",6)==0 || strncmp(buf,"#ifndef",7)==0) {
if (ifdefs) printf("nested #ifdefs: lines %d and %d\n",
ifdefLines[ifdefs-1],lines);
ifdefLines[ifdefs++]=lines;
continue;
}
if (strncmp(buf,"#endif",6)==0) {
if (!ifdefs)
printf("#endif without #ifdef: line %d\n",
lines);
else ifdefs--;
continue;
}
p=buf;
while (c=*p++) {
if (singleQuote) {
if (c=='\'' && (*(p-2)!='\\' || *(p-3)=='\\')) singleQuote--;
continue;
}
if (doubleQuote) {
if (c=='"' && (*(p-2)!='\\' || *(p-3)=='\\')) doubleQuote--;
continue;
}
if (c=='/' && *p=='*') {
if (comments) printf("nested comments: lines %d and %d\n",
commentLines[comments-1],lines);
commentLines[comments++]=lines;
}
if (c=='*' && *p=='/') {
if (!comments) printf("extra close comment: line %d\n",lines);
comments--;
}
if (comments || singleQuote || doubleQuote) continue;
if (c=='\'') singleQuote++;
if (c=='"') doubleQuote++;
if (c=='{') {
if (parens) printf("open brace inside parens: lines %d and %d\n",
parenLines[parens-1],lines);
braceLines[braces++]=lines;
}
if (c=='}') {
if (parens) printf("close brace inside parens: lines %d and %d\n",
parenLines[parens-1],lines);
if (!braces) printf("unmatched close brace: line %d\n",lines);
else braces--;
}
if (c=='(') {
parenLines[parens++]=lines;
}
if (c==')') {
if (!parens) printf("unmatched close paren: line %d\n",lines);
else parens--;
}
}
if (singleQuote) printf("unclosed single quote: line %d\n",lines);
if (doubleQuote) printf("unclosed double quote: line %d\n",lines);
singleQuote=doubleQuote=0;
}
printf("file: %s; lines: %d\n",*argv,lines);
while (ifdefs) printf("unclosed ifdef: line %d\n",ifdefLines[--ifdefs]);
while (comments)
printf("unclosed comment: line %d\n",commentLines[--comments]);
while (braces) printf("unclosed brace: line %d\n",braceLines[--braces]);
while (parens) printf("unclosed paren: line %d\n",parenLines[--parens]);
fclose(f);
}
}
int strncmp(x,y,n)
char *x, *y;
int n;
{
int c;
while (n--) {
if (c=*x-*y++) return (c);
if (*x++=='\0') return (0);
}
return (0);
}